package main

import (
	"fmt"

	"gitee.com/lilihli/regression"
)

func main() {
	// 原始数据 (标准格式)
	y := []float64{
		11.2, 13.4, 40.7, 5.3, 24.8, 12.7, 20.9, 35.7, 8.7, 9.6,
		14.5, 26.9, 15.7, 36.2, 18.1, 28.9, 14.9, 25.8, 21.7, 25.7,
	}

	// 标准格式 (每个样本一行)
	X_standard := [][]float64{
		{587000, 16.5, 6.2},
		{643000, 20.5, 6.4},
		{635000, 26.3, 9.3},
		{692000, 16.5, 5.3},
		{1248000, 19.2, 7.3},
		{643000, 16.5, 5.9},
		{1964000, 20.2, 6.4},
		{1531000, 21.3, 7.6},
		{713000, 17.2, 4.9},
		{749000, 14.3, 6.4},
		{7895000, 18.1, 6.0},
		{762000, 23.1, 7.4},
		{2793000, 19.1, 5.8},
		{741000, 24.7, 8.6},
		{625000, 18.6, 6.5},
		{854000, 24.9, 8.3},
		{716000, 17.9, 6.7},
		{921000, 22.4, 8.6},
		{595000, 20.2, 8.4},
		{3353000, 16.9, 6.7},
	}

	// 转置格式 (每个因子一行)
	X_transposed := [][]float64{
		{587000, 643000, 635000, 692000, 1248000, 643000, 1964000, 1531000, 713000, 749000, 7895000, 762000, 2793000, 741000, 625000, 854000, 716000, 921000, 595000, 3353000},
		{16.5, 20.5, 26.3, 16.5, 19.2, 16.5, 20.2, 21.3, 17.2, 14.3, 18.1, 23.1, 19.1, 24.7, 18.6, 24.9, 17.9, 22.4, 20.2, 16.9},
		{6.2, 6.4, 9.3, 5.3, 7.3, 5.9, 6.4, 7.6, 4.9, 6.4, 6.0, 7.4, 5.8, 8.6, 6.5, 8.3, 6.7, 8.6, 8.4, 6.7},
	}

	fmt.Println("=== 使用标准格式 (每个样本一行) ===")
	var residual regression.Residual
	var statistic regression.Statistic
	var vif regression.VIF
	result1, err := regression.New(X_standard, y).SetResidual(&residual).SetStatistic(&statistic).SetVIF(&vif).Run()
	if err != nil {
		fmt.Println(err)
		return
	}
	printResults(result1, &residual, &statistic, &vif)

	fmt.Println("\n=== 使用转置格式 (每个因子一行) ===")
	var residual2 regression.Residual
	var statistic2 regression.Statistic
	var vif2 regression.VIF
	result2, err := regression.New(X_transposed, y).SetResidual(&residual2).SetStatistic(&statistic2).SetVIF(&vif2).Run()
	if err != nil {
		fmt.Println(err)
		return
	}
	printResults(result2, &residual, &statistic, &vif)
}

func printResults(result *regression.RegressionResult, residual *regression.Residual, statistic *regression.Statistic, vif *regression.VIF) {
	fmt.Printf("截距 (Intercept): %.6f\n", result.Intercept)
	fmt.Println("\n估计系数 (Coefficients):")
	varNames := []string{"Inhabitants", "Percent with incomes below $5000", "Percent unemployed"}
	for i, coef := range result.Coefficients {
		fmt.Printf("  %s: %.6f\n", varNames[i], coef)
	}

	fmt.Println("\n标准误差 (Standard Errors):")
	for i := 1; i < len(statistic.StdErrors); i++ {
		fmt.Printf("  %s: %.6f\n", varNames[i-1], statistic.StdErrors[i])
	}

	fmt.Println("\nt 值 (t-values):")
	for i := 1; i < len(statistic.TValues); i++ {
		fmt.Printf("  %s: %.6f\n", varNames[i-1], statistic.TValues[i])
	}

	fmt.Println("\n方差膨胀系数 (VIFs):")
	for i, vif := range vif.VIFs {
		fmt.Printf("  %s: %.6f\n", varNames[i], vif)
	}

	fmt.Printf("\nR²: %.6f\n", statistic.RSquared)
	fmt.Printf("调整 R²: %.6f\n", statistic.AdjustedRSq)

	// 预测值和残差
	fmt.Println("\n预测值 (Predictions):")
	for i, pred := range residual.Predictions {
		fmt.Printf("  观测 %d: %.6f\n", i+1, pred)
	}

	fmt.Println("\n残差 (Residuals):")
	for i, res := range residual.Residuals {
		fmt.Printf("  观测 %d: %.6f\n", i+1, res)
	}

	// 解释 VIF
	fmt.Println("\nVIF 解释:")
	fmt.Println("  VIF < 5: 无显著多重共线性")
	fmt.Println("  5 ≤ VIF < 10: 中等多重共线性")
	fmt.Println("  VIF ≥ 10: 严重多重共线性")
	for i, vif := range vif.VIFs {
		level := "无显著多重共线性"
		if vif >= 10 {
			level = "严重多重共线性"
		} else if vif >= 5 {
			level = "中等多重共线性"
		}
		fmt.Printf("  %s: %s (VIF=%.2f)\n", varNames[i], level, vif)
	}
}
